using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.DriftBottle.Scene;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
namespace MicroMsg.Plugin.DriftBottle
{
	public class DriftBottle : PluginBase
	{
		private const string TAG = "DriftBottlePlugin";
		public const string PluginKey = "DriftBottlePlugin";
		public const long PluginID = 32769L;
		public const string PluginName = "DriftBottle";
		public const int PluginVersion = 256;
		public const string bNotFirstInDriftBottleKey = "bNotFirstInDriftBottle";
		public static PluginBase mInstance;
		private EventWatcher mWatchBConStoreChange;
		private object objWatch = new object();
		public override string mTitle
		{
			get
			{
				return strings.Contacts_Plugin_FloatBottle_Nickname;
			}
		}
		public override string mFFEntryTitle
		{
			get
			{
				return strings.Contacts_Plugin_FloatBottle_Nickname;
			}
		}
		public override BitmapSource mIcon
		{
			get
			{
				if (MMTheme.IsCoolBlackTheme())
				{
					return ImageCacheMgr.loadFromRes("Source/Plugin/DriftBottle/Resource/Img/plugin_setting_cool.png");
				}
				return ImageCacheMgr.loadFromRes("Source/Plugin/DriftBottle/Resource/Img/plugin_setting_normal.png");
			}
		}
		public override BitmapSource mIconHD
		{
			get
			{
				return HeadImgMgr.getHeadImage("floatbottle", true);
			}
		}
		public override BitmapSource mIconInList
		{
			get
			{
				return this.mIcon;
			}
		}
		public override string mDescription
		{
			get
			{
				return strings.Setting_Plugin_FloatBottle;
			}
		}
		public override string mAuthor
		{
			get
			{
				return "tencent";
			}
		}
		public DriftBottle()
		{
			this.mKey = "DriftBottlePlugin";
			this.mID = 32769L;
			this.mName = "DriftBottle";
			this.mVersion = 256;
			this.mIndexInTab = MainTabIndex.FindFriendTab;
			this.mIndexInGroup = 0;
			this.mIndexInPosition = 1;
			this.mProtocolPluginFlag = EPluginFlag.MM_BOTTLE_UNINSTALL;
			this.mDefaultInstalled = true;
			DriftBottle.mInstance = this;
			if (MMTheme.IsCoolBlackTheme())
			{
				base.mIconInFFEntry = new BitmapImage(new Uri("/Source/Plugin/DriftBottle/Resource/Img/DriftBottle_Icon.png", 2));
				return;
			}
			base.mIconInFFEntry = new BitmapImage(new Uri("/Source/Plugin/DriftBottle/Resource/Img/DriftBottle_Icon_light.png", 2));
		}
		public override PluginSettingBaseCtrl GetPluginSettingCtrl()
		{
			return new DriftBottlePluginSetCtrl();
		}
		public static DriftBottle getInstance()
		{
			return DriftBottle.mInstance as DriftBottle;
		}
		public override void onRegisterResult(RetConst retConst_0, int code)
		{
			Log.smethod_4("DriftBottlePlugin", string.Concat(new object[]
			{
				"on register result , ret = ",
				retConst_0,
				", code = ",
				code
			}));
		}
		public override bool onInitialize()
		{
			Log.smethod_4("DriftBottlePlugin", "initialize. ");
			this.registerEvent();
			using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
			{
				string pluginPath = StorageIO.getPluginPath("DriftBottle");
				string text = StorageIO.getPluginPath("DriftBottle") + "/voice";
				if (!userStoreForApplication.DirectoryExists(pluginPath))
				{
					userStoreForApplication.CreateDirectory(pluginPath);
				}
				if (!userStoreForApplication.DirectoryExists(text))
				{
					userStoreForApplication.CreateDirectory(text);
				}
			}
			DriftBottleServiceCenter.init();
			if (AccountMgr.ShowBottleInChat)
			{
				BConversationMgr.ShowBConvInMainPage(false);
			}
			else
			{
				BConversationMgr.HideBConvInMainPage();
			}
			return true;
		}
		public override bool onUnInitialize()
		{
			base.onUnInitialize();
			DriftBottle.mInstance = null;
			this.setBtlCnt(0);
			this.unRegisterEvent();
			return true;
		}
		public override bool onInstalled(InstallMode mode)
		{
			Log.smethod_4("DriftBottlePlugin", "on inistalled. ");
			return true;
		}
		public override bool onUninstalled(InstallMode mode)
		{
			Log.smethod_4("DriftBottlePlugin", "on uninstalled. ");
			BConversationMgr.DriftBottleClearAll();
			this.setBtlCnt(0);
			return true;
		}
		public override void onAppActivated()
		{
		}
		public override void onAppDeactivated()
		{
		}
		public override bool onParseChatMsg(ChatMsg chatMsg)
		{
			return this.onParseChatTalker(chatMsg.strTalker);
		}
		public override bool onParseChatTalker(string talker)
		{
			return talker.Contains("@bottle:");
		}
		private void toSetBottleHeadPage()
		{
			ToolFunction.GotoPageForUserControl("/Source/Plugin/DriftBottle/UI/SetDriftBottleHead.xaml", new object[]
			{
				typeof(FindFriendView)
			}, false);
		}
		private void toCompletProfile()
		{
			ToolFunction.GotoPageForUserControl("/Source/Plugin/Lbs/UI/LbsInputUserInfo.xaml", new object[]
			{
				strings.Float_Bottle_Title,
				"/Source/Plugin/DriftBottle/UI/DriftBottleMainPage.xaml"
			}, false);
		}
		private void btnDriftBottle_Click()
		{
			if (!ConfigMgr.getBool("bNotFirstInDriftBottle", false))
			{
				this.toSetBottleHeadPage();
				return;
			}
			if (AccountMgr.IsAcountInfoCompleted())
			{
				Log.smethod_5(base.GetType().ToString(), "Account info perfect, direct in Lbs list");
				ToolFunction.GotoPageForUserControl("/Source/Plugin/DriftBottle/UI/DriftBottleMainPage.xaml", null, false);
				return;
			}
			this.toCompletProfile();
		}
		public override object execute(int entryType, object args)
		{
			Log.smethod_4("DriftBottlePlugin", "plugin execute,   entrytype =" + entryType);
			if (entryType == 1)
			{
				return null;
			}
			if (entryType == 2)
			{
				return null;
			}
			if (entryType == 3)
			{
				if (base.isInstalledPlugin())
				{
					this.btnDriftBottle_Click();
				}
				else
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/PluginSettingPage.xaml", new object[]
					{
						this
					}, false);
				}
				return null;
			}
			if (entryType == 4)
			{
				this.btnDriftBottle_Click();
				return null;
			}
			return null;
		}
		public override void SetFFEntryCallBack(StandardUITaskDlg FFEntryCallBack)
		{
			this.mFFEntryCallBack = FFEntryCallBack;
			this.refreshBtlCnt();
		}
		private void setBtlCnt(int nCnt)
		{
			if (this.mFFEntryCallBack != null)
			{
				try
				{
					this.mFFEntryCallBack(FFEntryCtrlOpKind.OpKind_Cnt, nCnt);
				}
				catch
				{
				}
			}
		}
		private void registerEvent()
		{
			if (this.mWatchBConStoreChange == null)
			{
				this.mWatchBConStoreChange = new EventWatcher(this.objWatch, null, new EventHandlerDelegate(this.OnBConStoreChange));
				EventCenter.registerEventWatcher(EventConst.ON_BCONVERSATION_LIST_CHANGE, this.mWatchBConStoreChange);
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_DEL, this.mWatchBConStoreChange);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchBConStoreChange != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_BCONVERSATION_LIST_CHANGE, this.mWatchBConStoreChange);
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_BCONVERSATION_DEL, this.mWatchBConStoreChange);
				this.mWatchBConStoreChange = null;
			}
		}
		private void OnBConStoreChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.refreshBtlCnt();
		}
		private void refreshBtlCnt()
		{
			int unReadCountAll = BConversationMgr.getUnReadCountAll();
			this.setBtlCnt(unReadCountAll);
		}
	}
}
